package org.lx.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {

    public static String pdfToString(InputStream inputStream) {
        try (PDDocument document = PDDocument.load(inputStream)) {
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            return pdfTextStripper.getText(document);
        } catch (IOException e) {
            throw new PDFProcessingException("无法处理PDF", e);
        }
    }

    private static class PDFProcessingException extends RuntimeException {
        public PDFProcessingException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}
